package com.east.sng.controller;

import com.east.sng.entity.param.RoleParam;
import com.east.sng.entity.result.MenuTree;
import com.east.sng.entity.result.RoleFind;
import com.east.sng.service.MenuService;
import com.east.sng.service.RoleService;
import com.east.sng.utils.ValidationUtil;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * @author east
 * @date: 2021/1/5
 * @Description:
 **/
@Controller
@RequiresRoles("0")
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private MenuService menuService;

    /**
     * 角色信息页面跳转
     * @param pageNum
     * @param pageSize
     * @param model
     * @return
     */
    @RequiresPermissions("role:list")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String list(@RequestParam(name="flag",defaultValue="0")Integer flag,
                       @RequestParam(name="pn",defaultValue="1")Integer pageNum,
                       @RequestParam(name = "pSize", defaultValue = "5") Integer pageSize, Model model){
        model.addAttribute("page",PageHelper.startPage(pageNum,pageSize));
        model.addAttribute("roles",this.roleService.findRoles(flag));
        return "system/roleList";
    }

    /**
     * 角色信息录入
     * @param role
     * @param br
     * @return
     */
    @RequiresPermissions("role:add")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public String addRole(@Valid RoleParam role, BindingResult br){
        ValidationUtil.validateData(br);
        this.roleService.addRole(role);
        return "success";
    }

    /**
     * 角色信息修改页面跳转
     * @param roleId
     * @return
     */
    @RequiresPermissions(value = {"role:list","role:add"},logical = Logical.AND)
    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    @ResponseBody
    public RoleFind toEditRole(Integer roleId){
        return this.roleService.findRole(roleId);
    }

    /**
     * 角色信息修改
     * @param role
     * @param br
     * @return
     */
    @RequiresPermissions(value = {"role:list","role:add"},logical = Logical.AND)
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    @ResponseBody
    public String editRole(@Valid RoleParam role, BindingResult br){
        ValidationUtil.validateData(br);
        this.roleService.editRole(role);
        return "success";
    }

    /**
     * 角色信息删除
     * @param id
     * @return
     */
    @RequiresPermissions(value = {"role:list","role:add"},logical = Logical.AND)
    @RequestMapping(value = "/remove",method = RequestMethod.POST)
    @ResponseBody
    public String removeRole(Integer id){
        return this.roleService.removeRole(id)?"success":"fail";
    }

    /**
     * 角色信息恢复
     * @param id
     * @return
     */
    @RequiresPermissions(value = {"role:list","role:add"},logical = Logical.AND)
    @RequestMapping(value = "/recover",method = RequestMethod.POST)
    @ResponseBody
    public String recoverRole(Integer id){
        return this.roleService.recoverRole(id)?"success":"fail";
    }

    /**
     * 角色信息批量删除
     * @param ids
     * @return
     */
    @RequiresPermissions(value = {"role:list","role:add"},logical = Logical.AND)
    @RequestMapping(value = "/removes",method = RequestMethod.POST)
    @ResponseBody
    public String removeRoles(Integer[] ids){
        return this.roleService.batchRemoveRoles(Arrays.asList(ids))?"success":"fail";
    }

    /**
     * 角色信息批量恢复
     * @param ids
     * @return
     */
    @RequiresPermissions(value = {"role:list","role:add"},logical = Logical.AND)
    @RequestMapping(value = "/recovers",method = RequestMethod.POST)
    @ResponseBody
    public String recoverRoles(Integer[] ids){
        return this.roleService.batchRecoverRoles(Arrays.asList(ids))?"success":"fail";
    }

    /**
     * 分配角色菜单信息查询
     * @return
     */
    @RequiresPermissions(value = {"role:list","role:add"},logical = Logical.AND)
    @RequestMapping(value = "/assign",method = RequestMethod.GET)
    @ResponseBody
    public List<MenuTree> toAssign(Integer roleId){
        if (roleId==null) {
            return null;
        }
        //查询所有一级菜单
        List<MenuTree> firstMenu = menuService.findMenuByPid(0);
        //查询该角色分配的菜单
        List<Integer> roleAssignMenu = menuService.findAssignMenuIdsByRoleId(roleId);
        //给一级菜单判断二级菜单
        for (MenuTree m : firstMenu) {
            List<MenuTree> childMenu = menuService.findMenuByPid(m.getMenuId());
            for (Integer rm : roleAssignMenu) {
                if (m.getMenuId().equals(rm)) {
                    m.setChecked(true);
                    m.setOpen(true);
                }
                for (MenuTree cm : childMenu) {
                    if (cm.getMenuId().equals(rm)) {
                        cm.setChecked(true);
                        cm.setOpen(true);
                    }
                }
            }
            m.setChildren(childMenu);
        }
        return firstMenu;
    }

    /**
     * 分配角色菜单
     * @param roleId
     * @param menuIds
     * @return
     */
    @RequiresPermissions(value = {"role:list","role:add"},logical = Logical.AND)
    @RequestMapping(value = "/assign",method = RequestMethod.POST)
    @ResponseBody
    public String assign(Integer roleId, Integer[] menuIds){
        return this.roleService.assignRoleToMenu(roleId,Arrays.asList(menuIds))?"success":"fail";
    }

}
